<?php

class AppController extends Controller {
	//increase performance in many cases.
	//link:http://book.cakephp.org/view/814/persistModel
	//public  $persistModel = true;

    public  $components = array('Acl', 'Auth','Session', 'RequestHandler', 'Bayon', 'Email');
   	public  $helpers = array('Session', 'Html', 'Form', 'Js', 'Ajax', 'Bayon');

    public function beforeFilter() {
        //pr($this->data);
    	//set phon penh time zone
        date_default_timezone_set('Asia/Phnom_Penh');
        $this->Auth->fields = array('username' => 'email', 'password' => 'password');
        //set security
        Security::setHash('md5');
        //Configure AuthComponent
        //$this->Auth->allow('*');
        $this->Auth->authorize = 'actions';
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
        //Only user confirmed allow login
        $this->Auth->userScope = array('User.confirmed' => 1);
        $this->Auth->loginRedirect = array('/');
        
    }
    
    public function setLayout(){
    	if($this->Bayon->isLogging()){
    		if(isset($this->params['admin'])){
    			$this->layout = 'admin';
    		}else{
				$this->layout = 'user';
				$this->set('logged', true);
    		}
	}else{ 
		$this->layout = 'public';
		$this->set('logged', false);
	}
		// check ajax request
		$this->checkAjax();
        
    }
/**
 * Reserve name and keyword
 * 
 */
    public function reserveKeyword($keyword){

		$reserveWord= array('admin', 'avatars', 'company', 'jobs', 'login', 'logout', 'signin', 'product','service', 'signup', 'search');
		if( in_array($keyword, $reserveWord)){
			return true;
		}else{
			return false;
		}
		
    }
    public function checkPermission($userId){
        if($this->Bayon->userId() == $userId){
            return true;
        }else{
            return false;
        }
    }
    public  function checkAjax() {
        
        if ( $this->RequestHandler->isAjax() ) { 
        	Configure::write('debug', 0);
        	//$this->layout ='ajax';
            // this must exist: \app\views\layouts\json\ajax.ctp
            $this->RequestHandler->renderAs( $this, 'json' );
        }
    }
    public function sentEmail($from, $sentTo, $subject, $message){
        
    }
    public function sendNewUserMail($from, $sentTo, $subject, $message) {
    //$User = $this->User->read(null,$id);
    $this->Email->to = $sentTo;
    $this->Email->bcc = array('seap@bayonpage.com', 'sophy@bayonpage.com', 'titdara@bayonpage.com');
    $this->Email->subject = $subject;
    $this->Email->replyTo = 'sophy.prak@gmail.com';
    $this->Email->from = "Bayonpage.com <$from>";
    //$this->Email->template = 'simple_message'; // note no '.ctp'
    //Send as 'html', 'text' or 'both' (default is 'text')
    $this->Email->sendAs = 'Html'; // because we like to send pretty mail
    //Set view variables as normal
    //$this->set('User', $User);
    //Do not pass any args to send()
    $this->Email->send($message);
 }

}
?>